package com.zlcx.tz.live.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * <p></p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author zl
 * @version 1.0
 * @date Created in 2019/5/23 17:01
 * @copyright: Copyright (c) founders
 */

public class IPUtil {

    /**
     * IP地址转成long型数字
     *
     * @param strIp
     * @return
     */
    public static long ipToLong(String strIp) {
        String[] ip = strIp.split("\\.");
        return (Long.parseLong(ip[0]) << 24) + (Long.parseLong(ip[1]) << 16) + (Long.parseLong(ip[2]) << 8) + Long.parseLong(ip[3]);
    }

    /**
     * 将十进制整数形式转换成IPV4地址
     *
     * @param longIp
     * @return
     */
    public static String longToIP(long longIp) {
        StringBuffer sb = new StringBuffer("");
        // 直接右移24位
        sb.append(String.valueOf((longIp >>> 24)));
        sb.append(".");
        // 将高8位置0，然后右移16位
        sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16));
        sb.append(".");
        // 将高16位置0，然后右移8位
        sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8));
        sb.append(".");
        // 将高24位置0
        sb.append(String.valueOf((longIp & 0x000000FF)));
        return sb.toString();
    }


    /**
     * 获取本地IpV4地址
     * @return
     * @throws SocketException
     */
    public static String getIpV4Address() throws SocketException {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface current = interfaces.nextElement();
            if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
                continue;
            }
            Enumeration<InetAddress> addresses = current.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress addr = addresses.nextElement();
                if (addr.isLoopbackAddress() || !isValidIPv4(addr.getHostAddress()) ) {
                    continue;
                }
                return addr.getHostAddress();
            }
        }

        throw new SocketException("can't get local ipv4 address, interfaces are: " + interfaces);
    }

    /**
     * 校验是否是IpV4
     * @param ipV4Str
     * @return
     */
    public static boolean isValidIPv4(String ipV4Str) {
        if (ipV4Str ==null || ipV4Str.trim().length() == 0){
            return false;
        }

        int octet;
        int octets = 0;

        String temp = ipV4Str+".";

        int pos;
        int start = 0;
        while (start < temp.length() && (pos = temp.indexOf('.', start)) > start) {
            if (octets == 4) {
                return false;
            }
            try {
                octet = Integer.parseInt(temp.substring(start, pos));
            } catch (NumberFormatException ex) {
                return false;
            }
            if (octet < 0 || octet > 255) {
                return false;
            }
            start = pos + 1;
            octets++;
        }

        return octets == 4;
    }

}
